﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using YetAnotherChat.Common.Interfaces;

namespace YetAnotherChat.Common
{
    public abstract class EntityBase : IClonableForWriting
    {
        private bool _isCloned = false;


        public EntityBase()
        {
            //CreatedDate = System.Data.SqlTypes.SqlDateTime.MinValue.Value;
            //LastModified = System.Data.SqlTypes.SqlDateTime.MinValue.Value;
        }

        [Key]
        public Guid GuidId { get; set; }
        public DateTime? CreatedDate { get; set; }
        public string ModifiedBy { get; set; }
        public DateTime? LastModified { get; set; }
        public bool IsDeleted { get; set; }

        public bool IsCloned
        {
            get { return _isCloned; }
        }

        protected virtual void SetCloned()
        {
            _isCloned = true;
        }

        public virtual object Clone()
        {
            //get reposiroty instance from service locator
            //and re-get this object from database but enable tracking, compare timestamp (lastmodified) on it
            //to check for concurrency and return that new instance here!!!
            

            //EF supports attach entity to edit so just do simple clone now
            var clone = this.MemberwiseClone() as EntityBase;
            clone.SetCloned();
            return clone;
        }
    }
}
